--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.
		

]]


--Clientside includes
include('shared.lua')


for k, v in pairs(file.FindInLua("GModRP/gamemode/vgui/*.lua")) do

	include("vgui/"..v)
	
end


for k, v in pairs(file.FindInLua("GModRP/gamemode/derma/*.lua")) do

	include("derma/"..v)
	
end


Radiostations = {}
companyData = {}

--fonts
surface.CreateFont( "coolvetica", 20, 550, true, false, "GModRPFontOne" )
surface.CreateFont( "coolvetica", 35, 550, true, false, "NotifyFontOne" )


function GM:HUDPaint()

	self.BaseClass:HUDPaint()
	
	local person = LocalPlayer()
	
	if ( !person:Alive() || person:GetNWBool( "sleeping" ) ) then return end
	
	local numHealth = person:Health()
	local numEnergy = person:GetNWInt( "energy" )
	local numMoney = person:GetNWInt( "tehmoneylol" )
	local numStamina = person:GetNWInt( "stamina" )
	
	
	local trace = {}
	trace.start = person:EyePos()
	trace.endpos = trace.start + person:GetAimVector() * 85
	trace.filter = person
	local tr = util.TraceLine( trace )		
	
	local ent = tr.Entity

	local hudPos = 15
	local hw, ht = ScrW(), 26 
	
	surface.SetDrawColor( 255, 255, 255, 255 )	
	surface.DrawRect( 0, 0, hw, ht )
	
	for i = 0, 10 do
	
		surface.SetDrawColor( 0, 0, 0, 50 )	
		surface.DrawRect( 0, 0, hw, 2 + i * 2 )	

	end		
	
	surface.SetDrawColor( 0, 0, 0, 255  )
	surface.DrawOutlinedRect( 0, 0, hw, ht )		
	
	surface.SetTexture( surface.GetTextureID( "GModRP/icons/health" ) )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRect( hudPos, 3, 20, 20 )
	
	surface.SetTextColor( 255, 255, 255, 255 )
	surface.SetFont( "GModRPFontOne" )
	surface.SetTextPos( hudPos + 25, 3 )
	surface.DrawText( numHealth )
	
	hudPos = hudPos + 80
	
	surface.SetTexture( surface.GetTextureID( "GModRP/icons/hunger" ) )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRect( hudPos, 3, 20, 20 )
	
	surface.SetTextColor( 255, 255, 255, 255 )
	surface.SetFont( "GModRPFontOne" )
	surface.SetTextPos( hudPos + 25, 3 )
	surface.DrawText( numEnergy )	
	
	
	hudPos = hudPos + 80
	
	surface.SetTexture( surface.GetTextureID( "GModRP/icons/sprint" ) )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRect( hudPos, 3, 20, 20 )
	
	surface.SetTextColor( 255, 255, 255, 255 )
	surface.SetFont( "GModRPFontOne" )
	surface.SetTextPos( hudPos + 25, 3 )
	surface.DrawText( numStamina )	

	hudPos = hudPos + 80
	
	surface.SetTexture( surface.GetTextureID( "GModRP/icons/money" ) )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRect( hudPos, 3, 20, 20 )
	
	surface.SetTextColor( 255, 255, 255, 255 )
	surface.SetFont( "GModRPFontOne" )
	surface.SetTextPos( hudPos + 25, 3 )
	surface.DrawText( numMoney )	

	hudPos = hudPos + 80
	
	if person:IsOutside() then
	
		surface.SetTextPos( hudPos + 25, 3 )
		surface.SetTextColor( 255, 255, 255, 255 )	
		surface.SetFont( "GModRPFontOne" )
		surface.DrawText( "OUTSIDE " )	
		
	else
	
		surface.SetTextPos( hudPos + 25, 3 )
		surface.SetTextColor( 255, 255, 255, 255 )	
		surface.SetFont( "GModRPFontOne" )
		surface.DrawText( "INSIDE " )
		
	end	

	
	if !ent:IsValid() then return end
	
	if ( ent:GetNWString( "radiostation" ) != "" ) then
	
		draw.WordBox( 8, ScrW() / 2, ScrH() / 2, ent:GetNWString( "radiostation" ), "ChatFont", Color(50,50,75,100), Color(255,255,255,255) )	
	
	elseif ent:isOwnable() && !person:InVehicle() then
		
		if ent:GetNWString( "doorowner" ) != "" then
	
			surface.SetTextPos( ScrW() / 2, ScrH()/2 )
			surface.SetTextColor( 255, 255, 255, 255 )	
			surface.SetFont( "ChatFont" )
			surface.DrawText( ent:GetNWString( "doorowner" ) )

		else
			
			surface.SetTextPos( ScrW()/2, ScrH()/2 )
			surface.SetTextColor( 255, 255, 255, 255 )	
			surface.SetFont( "ChatFont" )
			surface.DrawText( "UNOWNED - PRESS F4 TO OWN" )
				
		end	
			
	end
		
end


function GM:HUDShouldDraw( name )

	for k, v in pairs( {"CHudHealth", "CHudBattery"} ) do
	
		if ( name == v ) then
		
			return false
			
		end
		
	end	
	
	return true
	
end


function Notify( str, color )

	local na = 255
	
	hook.Add( "HUDPaint", "GModRPNotify", function()

		draw.DrawText( tostring( str ), "NotifyFontOne", 10, 35, Color( color.r, color.g, color.b, na ), 0 )	
		
	end )
	
	timer.Simple( 1.5, function()
	
		timer.Create( "RemoveNotify", 0.1, 0, function()
		
			na = na - 5
			
			if na <= 0 then
			
				timer.Destroy( "RemoveNotify" )
				hook.Remove( "HUDPaint", "GModRPNotify" )
				
			end
			
		end )
		
	end )
	
end


function GM:CalcView( ply, origin, angles, fov ) 

	if ply:Alive() then

		if ( ply:Health() <= 30 ) then
		
			angles.roll = angles.roll + ( math.sin( RealTime() ) * 3 )
			
		end	
			
		if ply:GetNWBool( "sitting" ) then
		
			origin = origin + Vector( 0, 0, -10 )
			angles = angles
			
		end	
		
	end
	
	return self.BaseClass:CalcView( ply, origin, angles, fov )
	
end


function GM:RenderScreenspaceEffects()
	
	self.BaseClass:RenderScreenspaceEffects()
	
	local person = LocalPlayer()
	local tab = {}
	
	if !person:Alive() then

		tab[ "$pp_colour_addr" ] = 0
		tab[ "$pp_colour_addg" ] = 0
		tab[ "$pp_colour_addb" ] = 0
		tab[ "$pp_colour_brightness" ] = 10
		tab[ "$pp_colour_contrast" ] = 1
		tab[ "$pp_colour_colour" ] = 1
		tab[ "$pp_colour_mulr" ] = 0
		tab[ "$pp_colour_mulg" ] = 0
		tab[ "$pp_colour_mulb" ] = 0 
		
		DrawColorModify( tab )		

		return

	end
	
	if ( person:Health() <= 30 ) then
	
		DrawMotionBlur( 0.3, 1, 0.05 )
		
	elseif ( person:GetNWInt( "energy" ) <= 15 ) then

		tab[ "$pp_colour_addr" ] = 0
		tab[ "$pp_colour_addg" ] = 0
		tab[ "$pp_colour_addb" ] = 0
		tab[ "$pp_colour_brightness" ] = ( math.sin( RealTime() ) / 10 )
		tab[ "$pp_colour_contrast" ] = 1
		tab[ "$pp_colour_colour" ] = 1
		tab[ "$pp_colour_mulr" ] = 0
		tab[ "$pp_colour_mulg" ] = 0
		tab[ "$pp_colour_mulb" ] = 0 
		
		DrawColorModify( tab )		
	
	elseif ( person:GetNWInt( "stamina" ) <= 10 ) then
	
		tab[ "$pp_colour_addr" ] = 0
		tab[ "$pp_colour_addg" ] = 0
		tab[ "$pp_colour_addb" ] = 0
		tab[ "$pp_colour_brightness" ] = .3
		tab[ "$pp_colour_contrast" ] = 1
		tab[ "$pp_colour_colour" ] = 1
		tab[ "$pp_colour_mulr" ] = 0
		tab[ "$pp_colour_mulg" ] = 0
		tab[ "$pp_colour_mulb" ] = 0 
		
		DrawColorModify( tab )		
	
	end	
	
	if person:GetNWBool( "sleeping" ) && person:Alive() then
	
		tab[ "$pp_colour_addr" ] = 0
		tab[ "$pp_colour_addg" ] = 0
		tab[ "$pp_colour_addb" ] = 0
		tab[ "$pp_colour_brightness" ] = -10
		tab[ "$pp_colour_contrast" ] = 1
		tab[ "$pp_colour_colour" ] = 1
		tab[ "$pp_colour_mulr" ] = 0
		tab[ "$pp_colour_mulg" ] = 0
		tab[ "$pp_colour_mulb" ] = 0 
		
		DrawColorModify( tab )
		
	elseif person:GetNWBool( "ishigh" ) then
	
		DrawSharpen( 1, 3 )

	end

end


function GM:OnPlayerChat( player, text, teamchat, alive )
	
	if ( player:IsValid() ) then

		chat.AddText( Color( 255,255,255,255 ), "(OOC) ", team.GetColor( player:Team() ), player:Nick(), Color( 255,255,255,255 ), ": "..text )
		chat.PlaySound()
		
	else
	
		chat.AddText( Color( 255,255,255,255 ), "(OOC) ", Color( 255, 255, 255, 255 ), "Host", Color( 255,255,255,255 ), ": "..text )
		chat.PlaySound()	
		
	end	
	
	return true
	
end